package com.jader.activities;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jader.adapter.Choice_Address_Adapter;
import com.jader.beans.Address;
import com.jader.myviews.MyProgressDialog;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.app.Activity;
import android.content.Intent;

public class AddressChoice extends Activity {

	private int addressid;
	private List<Address> addresses = new ArrayList<Address>();
	private ListView address_listview;
	private Choice_Address_Adapter address_Adapter;
	private String url = JaderApplication.getIP() + "/Jader/client?type="
			+ JaderApplication.SELECT_ADDRESSBYUSERID + "&userid=";
	private MyProgressDialog dialog;
	private Button address_manager_address;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_choice);
		initViews();
	}

	private void initViews() {
		dialog = JaderApplication.getMyDialog(this);
		address_listview = (ListView) findViewById(R.id.listView_address);
		address_manager_address = (Button) findViewById(R.id.address_manager_address);
		addressid = getIntent().getIntExtra("addressid", 0);
		address_Adapter = new Choice_Address_Adapter(addresses, this, addressid);
		address_listview.setAdapter(address_Adapter);
		address_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent it = new Intent();
				it.putExtra("address", addresses.get(arg2));
				setResult(Activity.RESULT_OK, it);
				finish();

			}
		});
		address_manager_address.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(AddressChoice.this,
						AddressManagerActivity.class);
				startActivity(intent);
			}
		});

	}

	@Override
	protected void onResume() {
		if (JaderApplication.user == null) {
			JaderApplication.login(this);
		} else {
			sendRequest(url + JaderApplication.user.getUserid());
		}
		super.onResume();
	}

	private void sendRequest(String url) {
		dialog.show();
		JaderApplication.httpUtils.send(HttpMethod.GET, url,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						if (dialog.isShowing()) {
							dialog.dismiss();
						}
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						Gson gson = new Gson();
						Type type = new TypeToken<List<Address>>() {
						}.getType();
						List<Address> addresses = gson.fromJson(arg0.result,
								type);
						AddressChoice.this.addresses.addAll(addresses);
						address_Adapter.notifyDataSetChanged();
						if (dialog.isShowing()) {
							dialog.dismiss();
						}
					}
				});
	}

}
